package com.example.rk.config;

import com.example.rk.converter.MyDateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyAppConfig implements WebMvcConfigurer {
    @Value("${uploadfile.strorepath}")
    private String uploadStorePath;
    @Value("${uploadfile.strorepath.mapper}")
    private String uploadStorePathMapper;

//    @Value可以将配置文件的内容自动注入到属性内
//    @Value("${uploadfile.storepath}")
//    private String uploadStorePath;   //图标物理存储路径
//    @Value("${uploadfile.storepath.mapper}")
//    private String uploadStorePathMapper;
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(uploadStorePathMapper + "**").addResourceLocations("file:" + uploadStorePath);
    }

    @Bean   //声明这个方法返回一个bean实例，并把这个bean交给spring管理
    public MyDateConverter getMyDateConverter() {
        return new MyDateConverter();
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(getMyDateConverter());
    }
}
